Learn R Programming

hyperSpec (version 0.98-20140523)

[: Extract and Replace parts of hyperSpec objects

Description

These Methods allow to extract and replace parts of the hyperSpec object.

Usage

## S3 method for class 'hyperSpec':
[(x, i, j, l, ...,
    wl.index = FALSE, short = "[", date = NULL,
    user = NULL, drop = FALSE)

## S3 method for class 'hyperSpec': [[(x, i, j, l, ..., wl.index = FALSE, drop = FALSE)

## S3 method for class 'hyperSpec': $(x, name)

## S3 method for class 'hyperSpec': [(x, i, j, short = "[<-", date = NULL, user = NULL, \dots) <- value

## S3 method for class 'hyperSpec': [[(x, i, j, l, wl.index = FALSE, short = "[<-", date = NULL, user = NULL, \dots) <- value

## S3 method for class 'hyperSpec': $(x, name) <- value

Arguments

x
a hyperSpec Object
i
row index: selects spectra

[[ and code{[[<-} accept indexing with logical matrix or a n by 2 integer index matrix. In this case the indexing is done inside the spectra matrix. See the examples below.

j
selecting columns of x@data
l
selecting columns of the spectra matrix. If l is numeric, the default behaviour is treating l as wavelengths, not as indices.
wl.index
If TRUE (default), the value(s) in l are treated as column indices for the spectral matrix. Otherwise, the numbers in l are treated as wavelengths and the corresponding column indices are looked up first via
short,user,date
handed to logentry.
drop
For [[: drop unnecessary dimensions, see drop and Extract. Ignored for [, as otherwise invalid hyperSpec ob
...
ignored
name
name of the data column to extract. $spc yields the spectra matrix.
value
the replacement value

Value

  • For [, [<-, [[<-, and $<- a hyperSpec object,

    for [[ a matrix or data.frame, and

    for $ the column of the data.frame @data.

    x[[]] returns the complete spectra matrix.

    x$. returns the complete slot @data,

    x$.. returns the data.frame in @data but without the column @data$spc containing the spectra matrix.

Details

They work with respect to the spectra (rows of x), the columns of the data matrix, and the wavelengths (columns of the spectra matrix).

Thus, they can be used for selecting/deleting spectra, cutting the spectral range, and extracting or setting the data belonging to the spectra.

Convenient shortcuts for access of the spectra matrix and the data.frame in slot data are provided.

Extracting: [, [[, and $.

The version with single square brackets ([) returns the resulting hyperSpec object.

[[ yields data.frame of slot @data of that corresponding hyperSpec object returned with the same arguments by [ if columns were selected (i.e. j is given), otherwise the spectra matrix x@data$spc.

$ returns the selected column of the data.frame in slot @data.

Shortcuts. Three shortcuts to conveniently extract much needed parts of the object are defined:

x[[]] returns the spectra matrix.

x$. returns the complete slot @data, including the spectra matrix in column $spc, as a data.frame.

x$.. returns a data.frame like x$. but without the spectra matrix.

Replacing: [<-, [[<-, and $<-. ## S4 method for signature 'hyperSpec': x [i, j, l, short = NULL, \dots] <- value

## S4 method for signature 'hyperSpec': x [[i, j, l, wl.index = FALSE, short = NULL, \dots]] <- value

## S4 method for signature 'hyperSpec': x$name <- value

value gives the values to be assigned. For $, this can also be a list of the form list (value = value, label = label), with label containing the label for data column name.

[[<- replaces parts of the spectra matrix.

[<- replaces parts of the data.frame in slot x@data.

$<- replaces a column of the data.frame in slot x@data. The value may be a list with two elements, value and label. In this case the label of the data column is changed accordingly.

$..<- is again an abbreviation for the data.frame without the spectra matrix.

See Also

wl2i on conversion of wavelength ranges to indices.

drop and Extract on drop.

Examples

Run this code
## index into the rows (spectra) -------------------------------------
## make some "spectra"

## numeric index
plot (flu, "spc", lines.args = list (lty = 2))
plot (flu[1:3], "spc", add = TRUE, col = "red")     # select spectra
plot (flu[-(1:3)], "spc", add = TRUE, col = "blue") # delete spectra

## logic index
plot (flu, "spc", lines.args = list (lty = 2))
index <- rnorm (6) > 0
index
plot (flu[index], "spc", add = TRUE, col = "red")   # select spectra
plot (flu[!index], "spc", add = TRUE, col = "blue") # select spectra

## index into the data columns ---------------------------------------
range (chondro[[,"x"]])
colnames (chondro[[,1]])
dim (chondro[[,c(TRUE, FALSE, FALSE)]])
chondro$x


## the shortcut functions --------------------------------------------

## extract the spectra matrix
flu[[]]

## indexing via logical matrix
summary (flu [[flu < 125]])

## indexing the spectra matrix with index matrix n by 2
ind <- matrix (c (1, 2, 4, 406, 405.5, 409), ncol = 2)
ind
flu [[ind]]

ind <- matrix (c (1, 2, 4, 4:6), ncol = 2)
ind
flu [[ind, wl.index = TRUE]]

pca <- prcomp (flu[[]])

## result is data.frame, if j is given:
result <- flu [[, 1:2, 405 ~ 410]]
result
class (result)
colnames (result)

## extract the data.frame including the spectra matrix
flu$.
dim(flu$.)
colnames (flu$.)
flu$.$spc

calibration <- lm (spc ~ c, data = flu[,,450]$.)
calibration

flu$..
colnames (flu$..)
## replacement functions
spc <- flu
spc$.
spc[, "c"] <- 16 : 11
## be careful:
plot (spc)
spc [] <- 6 : 1
spc$..
plot (spc)
spc <- flu [,, 405 ~ 410]
spc [[]]
spc [[3]] <- -spc[[3]]
spc [[]]
spc [[,,405 : 410]] <- -spc[[,,405 : 410]]
spc [[]]
spc [[,,405 ~ 410]] <- -spc[[,,405 ~ 410]]

## indexing with logical matrix
spc <- flu [,, min ~ 410]
spc < 125
spc [[spc < 125]] <- NA
spc [[]]

## indexing with n by 2 matrix
ind <- matrix (c (1, 2, 4, 406, 405.5, 409), ncol = 2)
ind
spc [[ind]] <- 3
spc [[]]

ind <- matrix (c (1, 2, 4, 4:6), ncol = 2)
ind
spc [[ind, wl.index = TRUE]] <- 9999
spc [[]]
spc$.
spc$z <- 1 : 6
spc
spc$z <- list (1 : 6, "z / a.u.")

Run the code above in your browser using DataLab